/**
 * Created by showtan on 2018/12/27.
 * 地图测试
 */

import { MapView } from 'react-native-amap3d';

// let time = (Date.parse(new Date()) / 1000).toString(); // 避免后台运行任务重名
export default class MapTest extends Component {
  static navigationOptions = ({ navigation }) => ({
    title: '地图'
  });
  constructor(props) {
    super(props);
    this.state = {
      lat: 0.001
    }
  }
  componentWillMount() {
    // time = (Date.parse(new Date()) / 1000).toString();
    // if (Platform.OS === 'android') {
    //   const backgroundJob = {
    //     jobKey: `backgroundDownloadTask${time}`,
    //     job: () => this.setState({ lat: this.state.lat + 0.001 })
    //   };
    //   BackgroundJob.register(backgroundJob);
    // } else {
    //   this.timer = setInterval(() => this.setState({ lat: this.state.lat + 0.001 }), 2500);
    // };
    //监听状态改变事件
    // AppState.addEventListener('change', this.handleAppStateChange);
    BackgroundTimer.runBackgroundTimer(() => { this.setState({ lat: this.state.lat + 0.001 }); }, 3000);

  }
  //状态改变响应
  // handleAppStateChange = (appState) => {
  //   console.log('当前状态为:' + appState);
  // }

  componentDidMount() {
    // !isIOS && BackgroundJob.schedule({
    //   jobKey: `backgroundDownloadTask${time}`, //后台运行任务的key
    //   period: 2000,                     //任务执行周期
    //   exact: true,                     //安排一个作业在提供的时间段内准确执行
    //   allowExecutionInForeground: true,//允许任务在前台执行
    // });
  }
  componentWillUnmount() {
    // !isIOS && BackgroundJob.cancelAll();
    // AppState.removeEventListener('change', this.handleAppStateChange); this.timer && clearInterval(this.timer);
    BackgroundTimer.stopBackgroundTimer();
  }
  render() {
    console.log('get', YXStorage.get('location'))
    return (
      <View style={styles.container}>
        {/* <MapView
          mapType='standard' // 地图类型 standard 标准 satellite 卫星 navigation 导航 night 夜间 bus 公交
          zoomLevel={15}  // 放大等级
          style={StyleSheet.absoluteFill} // Android不加地图不显示
          coordinate={{ // 中心点
            latitude: YXStorage.get('location').lat,
            longitude: YXStorage.get('location').long,
          }}>
          <MapView.Marker // 标点
            draggable
            title='这是一个可拖拽的标记'
            onDragEnd={({ nativeEvent }) =>
              console.log(`${nativeEvent.latitude}, ${nativeEvent.longitude}`)}
            coordinate={{
              latitude: YXStorage.get('location').lat,
              longitude: YXStorage.get('location').long,
            }}
          />
          <MapView.Marker
            draggable
            title='这是一个可拖拽的标记'
            onDragEnd={({ nativeEvent }) =>
              console.log(`${nativeEvent.latitude}, ${nativeEvent.longitude}`)}
            coordinate={{
              latitude: YXStorage.get('location').lat - this.state.lat,
              longitude: YXStorage.get('location').long,
            }}
          />
        </MapView> */}
        <MapView
          locationEnabled
          onLocation={({ nativeEvent }) =>
            console.log(`${nativeEvent.latitude}, ${nativeEvent.longitude}`)}
        />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#ededed'
  },
});